using System;
using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using System.IO.Ports;
using System.Text;
using System.Collections;

#if MINI
using SecretLabs.NETMF.Hardware.NetduinoMini;
#else
using SecretLabs.NETMF.Hardware.Netduino;
#endif

namespace codebase.UI
{
    public class Clock : Control
    {
        public delegate int DataGrabberDelegate();

        public string Name;
        public string Unit;

        public int BorderColor;
        public int ForeColor;
        public int NameColor = ColorExtensions.RGB16(0,0,0);
        public int HandColor;
        public int FaceColor;

        public int Padding = 1;

        public int Max = 100;
        public int Min = 0;
        public int Value = 66;
        public int Size = 0;

        public int incrementCount = 10;
        public int incrementSize = 3;
        public bool drawIncrements = true;

        public DataGrabberDelegate DataGrabberFunction;

        public Clock(int x, int y, int w, int h, int bgc, int bc, int fc, int hc,int fac, Window parent)
        {
            Init(x, y, w, h, bgc, bc, fc,hc,fac, parent);
        }

        void Init(int x, int y, int w, int h, int bgc, int bc, int fc, int hc,int fac, Window parent)
        {
            Parent = parent;

            Left = x * Parent.screen.CharacterWidth;
            Top = y * Parent.screen.CharacterHeight;
            Width = w * Parent.screen.CharacterWidth;
            Height = h * Parent.screen.CharacterHeight;
            refreshSize();

            BackgroundColor = bgc;
            BorderColor = bc;
            ForeColor = fc;
            HandColor = hc;
            FaceColor = fac;
        }

        public void refreshSize()
        {
            Size = Math.Min(Width, Height);
        }

        int handSize = 0;
        int centerX = 0;
        int centerY = 0;

        protected override void DrawAll()
        {
            Parent.screen.DrawFilledRectangle(Left + Parent.Left + Padding, Top + Parent.Top + Padding, Left + Width + Parent.Left - Padding, Top + Height + Parent.Top - Padding, BackgroundColor);
            Parent.screen.DrawRectangle(Left + Parent.Left + Padding, Top + Parent.Top + Padding, Left + Width + Parent.Left - Padding, Top + Height + Parent.Top - Padding, BorderColor);

            handSize = Size / 2 - Padding * 4;
            centerX = Left + Parent.Left + (Size / 2);
            centerY = Top + Parent.Top + (Size / 2);

            Parent.screen.DrawFilledCircle(centerX, centerY, handSize, FaceColor);
            Parent.screen.DrawCircle(centerX, centerY, handSize, BorderColor);

            if (drawIncrements)
            {
                for (int i = 0; i < incrementCount; i++)
                {
                    double angle = (Max - (i * ((Max - Min) / incrementCount))) * 360 / (Max - Min) * Math.PI / 180;

                    int ix = (int)(handSize * Math.Sin(angle));
                    int iy = (int)(handSize * Math.Cos(angle));
                    int ix2 = (int)((handSize - incrementSize) * Math.Sin(angle));
                    int iy2 = (int)((handSize - incrementSize) * Math.Cos(angle));

                    Parent.screen.DrawLine(ix + centerX, iy + centerY, ix2 + centerX, iy2 + centerY, BorderColor);
                }
            }

            Draw();
        }

        protected override void Draw()
        {            
            double angle = (Max - Value) * 360 / (Max - Min) * Math.PI / 180;

            int x = (int)(drawIncrements ? ((handSize - incrementSize) * Math.Sin(angle)) : handSize * Math.Sin(angle));
            int y = (int)(drawIncrements ? ((handSize - incrementSize) * Math.Cos(angle)) : handSize * Math.Cos(angle));

            Parent.screen.DrawLine(centerX, centerY, x + centerX, y + centerY, BorderColor);

            Parent.screen.DrawFilledCircle(centerX + x, centerY + y, 2, HandColor);
            Parent.screen.DrawCircle(centerX + x, centerY + y, 2, BorderColor);

            Parent.screen.SPE.TextWidth(3);
            Parent.screen.SPE.TextHeight(3);

            string txt = Value.ToString() + Unit;
            int ox = (txt.Length * Parent.screen.CharacterWidth) / 2;
            int oy = Parent.screen.CharacterHeight / 2;

            Parent.screen.DrawText(txt, Left + Parent.Left + Size + Padding, centerY - oy, true, 0, NameColor);

            Parent.screen.SPE.TextWidth(1);
            Parent.screen.SPE.TextHeight(1);
        }
    }

}
